package com.example.activitidemo.controller.event;

import com.example.activitidemo.dto.Event;
import com.example.activitidemo.dto.EventVO;
import com.example.activitidemo.dto.User1;
import com.example.activitidemo.service.event.AssigneeService;
import com.example.activitidemo.service.event.EventService;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class EventTurnController {
    @Autowired
    private EventService eventService;
    @Autowired
    private AssigneeService assigneeService;

    /**
     * 跳转到事件列表页，默认也是事件列表
     */
    @GetMapping({"/", "index"})
    public String turnEventPage(Map<String, Object> map, HttpServletRequest request) {
        Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        String loginName = null;
        if (assertion != null) {
            AttributePrincipal principal = assertion.getPrincipal();
            loginName = principal.getName();
            System.out.println("访问者:" + loginName);
        }
        /*
         事件列表
         */
        List<Event> all = eventService.findAll();
        List<EventVO> list = new ArrayList<>();
        for (Event event : all) {
            EventVO eventVO = new EventVO();
            //使用静态工厂方法来进行赋值
            eventVO.assigneeName(event.getAssigneeName())
                    .day(event.getDay())
                    .id(event.getId())
                    .content(event.getContent())
                    .finishTime(event.getFinishTime())
                    .date(event.getDate());
            list.add(eventVO);
        }

        map.put("eventList", list);

        /*
        人的列表
         */
        List<User1> user1List = assigneeService.findAll();
        map.put("userList", user1List);

        return "event/event-list";
    }

    @GetMapping("/event-save-page")
    public String turnSaveEventPage(Map<String, Object> map) {
        List<User1> all = assigneeService.findAll();
        map.put("userList", all);
        return "event/event-save-page";
    }
}
